﻿package org.puremvc.as3.multicore.utilities.assetLoader.model
{
	/**

	* ...
	* @author neil manuell 
	*/
	
	// TODO: state property for the IAssetInfo, init, queued, loaded and deconstructed
	// TODO: add teh queue into the getAsset functionality of the Loader
	
	import flash.events.Event;
	
	import org.puremvc.as3.multicore.patterns.proxy.Proxy;
	

	import org.puremvc.as3.multicore.utilities.assetLoader.interfaces.IAssetInfo;
	import org.puremvc.as3.multicore.utilities.assetLoader.etypes.LoaderNotificationNames;
	

	public class LoaderProxy extends Proxy
	{
		
		public static const NAME:String = "LoaderProxy";
		

		public function LoaderProxy() 
		{
			super(NAME, new Loader());
			init()
		}
		
		/**
		 * indicates whether the load que is active
		 */
		public function get isActive():Boolean{
			return assetLoader.isActive;
		}
		
		/**
		 * the number of items left in the load que
		 */
		public function get queLength():int{
			return assetLoader.queLength;
		}
		
		/**
		 * the function that defines the load que order
		 */
		public function set queSortFunction(param:Function):void
		{
			assetLoader.queSortFunction = param;
		}
		
		/**
		 * defines whether the load que should automatically start when an item has been added
		 */
		public function get autoLoad():Boolean
		{
			return assetLoader.autoLoad;
		}
		
		/**
		 * @private
		 */
		public function set autoLoad(param:Boolean):void
		{
			assetLoader.autoLoad = param;
		}

		/**
		 * returns the IAssetInfo instance associated with the url.  
		 * If one doesn't exist, it will create one.  You have to add this to the
		 * loadQue with the addToQue() method.
		 * @param url the url of the asset to be loaded
		 * @return 
		 * 
		 */
		public function getAsset(url:String):IAssetInfo
		{
			return assetLoader.getAsset(url);
		}
		
		/**
		 * indicates whether the item indicated by the url has loaded yet. 
		 * @param url
		 * @return 
		 * 
		 */
		public function hasLoaded(url:String):Boolean
		{
			return assetLoader.hasLoaded(url);
		}
		
		/**
		 * deletes the asset indicated by the url registery. 
		 * This will not remove the item from load que.
		 * @param url
		 * 
		 */
		public function deleteAsset(url:String):void
		{
			assetLoader.deleteAsset(url)
		}
		
		/**
		 * This adds an IAssetInfo to the load que,  aquired from the getAsset() method.
		 * @param param
		 * 
		 */
		public function addToQue(param:IAssetInfo):void{
			
			assetLoader.addToQue(param)
		}
		
		/**
		 * Stops the load que 
		 * 
		 */
		public function stopQue():void{
			
			assetLoader.stopQue();
			
		}
		/**
		 * Starts the load que 
		 * 
		 */
		public function startQue():void{
			
			assetLoader.startQue();
		}
		
		/**
		 * removes all assets from the registray and the loadQue 
		 * 
		 */
		public function deconstruct():void
		{
			assetLoader.deconstruct();
		}
		
		/**
		 * Registers a client with the Loader along with the id that maps to it.
		 * The clientType property of an IAssetInfo must be set to this before adding it to the que
		 * @param id the id that maps to the client
		 * @param c the client as a class
		 * 
		 */
		public function registerClient(id:String, c:Class):void
		{
			assetLoader.registerClient(id, c)
		}
		
		/**
		 * Removes the client mapped with the id 
		 * @param id
		 * 
		 */
		public function removeClient(id:String):void
		{
			assetLoader.removeClient(id);
		}
		
		private function init():void
		{
			assetLoader.addEventListener(LoaderNotificationNames.ITEM_COMPLETED, relayEvent)
			assetLoader.addEventListener(LoaderNotificationNames.ITEM_STARTED, relayEvent)
			assetLoader.addEventListener(LoaderNotificationNames.QUE_COMPLETED, relayEvent)
			assetLoader.addEventListener(LoaderNotificationNames.QUE_PROGRESS, relayEvent)
			assetLoader.addEventListener(LoaderNotificationNames.ITEM_IOERROR, relayEvent)
		}
		
		private function get assetLoader():Loader{
			return Loader(getData());
		}
		
		private function relayEvent(event:Event):void
		{
			sendNotification(event.type, event)
		}
		
	}
	
}


	
	
	
	
	